﻿using System;
using System.Collections.Generic;

namespace SW.Lite.Model
{
    [Serializable]
    public class ColumnModel
    {
        /// <summary>
        /// </summary>
        public ColumnModel()
        {
        }

        /// <summary>
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="parentId">父级ID</param>
        /// <param name="code">编码</param>
        /// <param name="title">栏目名称</param>
        /// <param name="type">类型</param>
        /// <param name="modelId">模型ID</param>
        /// <param name="modelCode">模型编码</param>
        /// <param name="navigateUrl">访问Url</param>
        /// <param name="sort">排序号</param>
        public ColumnModel(int id, int parentId, string code, string title, int type, int modelId, string modelCode,
            string navigateUrl, int sort)
        {
            Id = id;
            ParentID = parentId;
            Code = code;
            Title = title;
            Type = type;
            ModelId = modelId;
            ModelCode = modelCode;
            NavigateUrl = navigateUrl;
            Sort = sort;
        }

        #region 属性

        /// <summary>
        ///     ID
        /// </summary>
        public int Id { get; set; }


        /// <summary>
        ///     父级ID
        /// </summary>
        public int ParentID { get; set; }


        /// <summary>
        ///     编码
        /// </summary>
        public string Code { get; set; }


        /// <summary>
        ///     栏目名称
        /// </summary>
        public string Title { get; set; }


        /// <summary>
        ///     类型
        /// </summary>
        public int Type { get; set; }


        /// <summary>
        ///     模型ID
        /// </summary>
        public int ModelId { get; set; }


        /// <summary>
        ///     模型编码
        /// </summary>
        public string ModelCode { get; set; }


        /// <summary>
        ///     访问Url
        /// </summary>
        public string NavigateUrl { get; set; }


        /// <summary>
        ///     排序号
        /// </summary>
        public int Sort { get; set; }

        /// <summary>
        ///     子栏目
        /// </summary>
        public ICollection<ColumnModel> Children { get; set; }

        #endregion
    }

    /// <summary>
    ///     Class ColumnComparer.
    /// </summary>
    public class ColumnComparer : IComparer<ColumnModel>
    {
        /// <summary>
        ///     Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <param name="x">The first object to compare.</param>
        /// <param name="y">The second object to compare.</param>
        /// <returns>
        ///     A signed integer that indicates the relative values of <paramref name="x" /> and <paramref name="y" />, as
        ///     shown in the following table.Value Meaning Less than zero<paramref name="x" /> is less than <paramref name="y" />
        ///     .Zero<paramref name="x" /> equals <paramref name="y" />.Greater than zero<paramref name="x" /> is greater than
        ///     <paramref name="y" />.
        /// </returns>
        public int Compare(ColumnModel x, ColumnModel y)
        {
            if (x == null)
                return y == null ? 0 : -1;

            if (y == null)
                return 1;

            if (x.ParentID == 0 && y.ParentID == 0)
                return x.Sort.CompareTo(y.Sort);
            if (x.ParentID == 0 && y.ParentID != 0)
                return 1;
            if (x.ParentID != 0 && y.ParentID == 0)
                return -1;
            if (x.ParentID != 0 && y.ParentID != 0)
                return x.Sort.CompareTo(y.Sort);
            return 0;
        }
    }
}